<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace switcher\filters;

/**
 * The <b>CompressionFilter</b> class is used to compress the response when it meets
 * certain conditions. Compression is effective if the User Agent claims to support
 * such encodings and the response matches one of the declared MIMEs.
 * 
 * <p>The <b>CompressionFilter</b> is useful when you are not granted the rights to
 * configure your HTTP server or your PHP installation. You might also want to embed
 * such configuration within your application to increase portability or centralize
 * configuration.<p>
 * 
 * <p>The <b>CompressionFilter</b> should be handled carefully. It is provided to help
 * reduce bandwidth consumption at the cost of a small CPU load. Therefore, you should
 * use it to compress contents such as text. An image generated on the fly (JPG, PNG...)
 * for example is not suited for compression.</p>
 * 
 * <p>XML declaration :
 * <code>
 * <filters>
 *     <filter type="switcher\filters\CompressionFilter">
 *         <level>5</level>
 *         <mimes>application/csv</mimes>
 *         <mimes>application/xml</mimes>
 *         <mimes>application/*+xml</mimes>
 *     </filter>
 *     <filter type="switcher\filters\CompressionFilter">
 *         <level>9</level>
 *         <mimes>text/*</mimes>
 *     </filter>
 * </filters>
 * </code>
 * 
 * @author Dri <switcher.framework@gmail.com>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 * @package switcher\filters
 * @since 1.0.0
 */
final class CompressionFilter extends \switcher\filters\ResponseFilter
{
	/**
	 * The level of compression
	 * @var integer
	 */
	protected $level = 5;
	
	/**
	 * The supported MIMEs, can contain jokers
	 * @var mixed
	 */
	protected $mimes = 'text/*';
	
	/**
	 * Supported encodings and their respective functions
	 * @var array
	 */
	private static $_SUPPORTED_ENCODINGS = array(
		'gzip'    => 'gzencode',
		'deflate' => 'gzdeflate'
	);
	
	/**
	 * Search for a compatible encoding and use it to compress the output. Nothing is done
	 * if the HTTP response already declares a content encoding.
	 * 
	 * @param string $response The original response
	 */
	public function doFilterResponse($response)
	{
		if ( \switcher\utils\HttpHelper::hasContentEncoding() || !$this->isMimeSupported() )
		{
			return $response;
		}
		
		$acceptedEncodings = \switcher\utils\HttpHelper::getAcceptedEncodings();
		
		foreach ($acceptedEncodings as $encoding)
		{
			if ( array_key_exists($encoding, self::$_SUPPORTED_ENCODINGS) )
			{
				$function = self::$_SUPPORTED_ENCODINGS[$encoding];
				$compressedResponse = @$function($response, $this->level);
				
				if ($compressedResponse !== false)
				{
					header('Content-Encoding: ' . $encoding);
					return $compressedResponse;
				}
			}
		}
		
		return $response;
	}
	
	/**
	 * Check whether the response's MIME is supported by this filter.
	 */
	private function isMimeSupported()
	{
		if ( !is_array($this->mimes) )
		{
			$this->mimes = array($this->mimes);
		}
		
		$responseMime = \switcher\utils\HttpHelper::getContentTypeMime();
		
		foreach ($this->mimes as $mime)
		{
			if ( fnmatch($mime, $responseMime) )
			{
				return true;
			}
		}
		
		return false;
	}
}

?>